import re
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import make_interp_spline

rg = re.compile(r'\$\$.*?\$\$', re.S)
size_rg = re.compile(r'(-[^-]*-)', re.S)
time_para_rg = re.compile(r'\`.*?\`', re.S)
time_serial_rg = re.compile(r'\@.*?\@', re.S)
rate_rg = re.compile(r'\#.*?\#', re.S)
filepath = 'result_bubble_mpi.txt'

def get_file (filepath):
    print('读取文件 ' + filepath)
    fp = open(filepath, mode='r', encoding='utf-8')
    if fp == None:
        print('读取文件错误')
        exit(2)
    return rg.findall(fp.read())


def parse_data (data):
    parse_res = []
    for d_loop in data:
        size = eval(size_rg.findall(d_loop)[0].strip('-'))
        time_para = eval(time_para_rg.findall(d_loop)[0].strip('`'))
        time_serial = eval(time_serial_rg.findall(d_loop)[0].strip('@'))
        rate = eval(rate_rg.findall(d_loop)[0].strip('#'))
        # print([size, time_para, time_serial, rate])
        parse_res.append([size, time_para, time_serial, rate])
        # print(rate)
    return parse_res

data_list = parse_data(get_file(filepath))
print('获取数据成功，数据如下, 即将开始数据格式化', data_list[:5], sep='\n')
data_frame = pd.DataFrame(data_list)
print(data_frame)
plt.legend()